#include "RobotItem.h"
#include <QtWidgets>

std::ostream &operator << (std::ostream& os, const RobotBaseData& data) {
//    std::streamsize width = os.width();
//    os.width(10);
//    std::setiosflags(std::ios::left);
    int width = 10;
    std::cout << "{" << std::endl;
        std::cout << std::setw(width) << "num:" << data.num << std::endl;
        std::cout << std::setw(width) << "net:" << data.net << std::endl;
        std::cout << std::setw(width) << "x:" << data.x << std::endl;
        std::cout << std::setw(width) << "y:" << data.y <<  std::endl;
        std::cout << std::setw(width) << "theta:" << data.theta << std::endl;
        std::cout << std::setw(width) << "p:" << data.p << std::endl;
        std::cout << std::setw(width) << "v:" << data.v << std::endl;
        std::cout << std::setw(width) << "wv:" << data.wv << std::endl;
        std::cout << std::setw(width) << "ele:" << data.ele << std::endl;
        switch (data.state) {
        case (RobotRunningState::Ready) : {
            std::cout << std::setw(width) << "state:Ready" << std::endl;
            break;
        }
        case (RobotRunningState::Waiting) : {
            std::cout << std::setw(width) << "state:Waiting" << std::endl;
            break;
        }
        case (RobotRunningState::Pickup) : {
            std::cout << std::setw(width) << "state:Pickup" << std::endl;
            break;
        }
        case (RobotRunningState::Delivery) : {
            std::cout << std::setw(width) << "state:Delivery" << std::endl;
            break;
        }
        case (RobotRunningState::Back) : {
            std::cout << std::setw(width) << "state:Back" << std::endl;
            break;
        }
        default:
            std::cout << std::setw(width) << "No state" << std::endl;
        }

        std::cout << std::setw(width) << "a:" << data.a << std::endl;
        std::cout << std::setw(width) << "b:" << data.b << std::endl;
    std::cout << "}" << std::endl;

    return os;
}

int RobotItem::m_itemCount = 0;

RobotItem::RobotItem(int id, QRectF rect, int x, int y, QGraphicsItem *parent) :
    QObject(),
    QGraphicsItem(parent)
{
    //1.设置坐标偏移
    m_coordinateOffset = QPoint(rect.x(), rect.y());
    //2.使用传参进行初始化
    m_data.num = id;
    m_data.x = x;
    m_data.y = y;
    //3.设置位置
    setRobotData(m_data);
    setFlags(ItemIsSelectable);
    setAcceptHoverEvents(true);
    //4.itemCount计数
    m_itemCount++;
    qDebug() << "ItemCount: " << m_itemCount;
}

RobotItem::RobotItem(QRectF rect, const RobotBaseData &data, QGraphicsItem *parent) :
    QObject(),
    QGraphicsItem(parent)
{
    //1.设置坐标偏移
    m_coordinateOffset = QPoint(rect.x(), rect.y());
    //2.使用数据进行初始化
    m_data = data;
    //3.设置位置
    setRobotData(m_data);
    setFlags(ItemIsSelectable);
    setAcceptHoverEvents(true);
    //4.itemCount计数
    m_itemCount++;
    qDebug() << "ItemCount: " << m_itemCount;
}

RobotItem::~RobotItem()
{
    m_itemCount--;
    qDebug() << "ItemCount: " << m_itemCount;
}

QRectF RobotItem::boundingRect() const
{
    return QRectF(m_itemRect + QMargins(-0, 20, 20, 20));
}

QPainterPath RobotItem::shape() const
{
    QPainterPath path;
    path.addRect( boundingRect());
    return path;
}

void RobotItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget);

    QColor fillColor = m_color;
    if (option->state & QStyle::State_MouseOver)
        fillColor = fillColor.lighter(125);


    QPen oldPen = painter->pen();
    QPen pen = oldPen;
    int width = 0;
    if (option->state & QStyle::State_Selected)
        width += 2;

    pen.setWidth(width);
    QBrush b = painter->brush();
    painter->setBrush(QBrush(fillColor.darker(option->state & QStyle::State_Sunken ? 120 : 100)));

    painter->drawEllipse(QPoint(0, 20), 20, 20);
    painter->drawRect( m_itemRect );
    painter->drawText(QPointF(-5, 5), QString::asprintf("%d", m_data.num));
    painter->setBrush(b);
}

void RobotItem::setRobotData(const RobotBaseData &data)
{
    m_data = data;
    setPos(QPoint(data.x, data.y) + m_coordinateOffset);
    setRotation(data.theta);
    //发送数据改变
    emit dataChanged(m_data);
}

void RobotItem::setColor(const QColor &color)
{
    m_color = color;
}

void RobotItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void RobotItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->modifiers() & Qt::ShiftModifier) {
        update();
        return;
    }
    QGraphicsItem::mouseMoveEvent(event);
}

void RobotItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
    update();
}
